﻿//(function() {
//    
//})();
CKEDITOR.plugins.add("autosave", {
    version: 1.0,
    init: function(editor) {
        editor.addCommand('autosave', {
            exec: function(editor) {
                var data;
                try {
                    data = editor.getData();
                } catch (e) {
                    alert('保存失败');
                    return;
                }

                var height = editor.container.$.clientHeight;
                var width = editor.container.$.clientWidth;
                $('#' + tempEditorName).children().remove();
                $('#' + tempEditorName).append($.parseHTML(data));
                var $_data = $('#' + tempEditorName);
                // 替换所有smartdata 的id
                var findAllSmartDatas = $_data.find('span.h-card');
                for (var i = 0; i < findAllSmartDatas.length; i++) {
                    var smartData = $(findAllSmartDatas[i]);
                    var id = smartData.attr('id');
                    if (id && id.indexOf('simpledata') >= 0) {
                        smartData.attr('id', ck.util.randomString(16, 'aA'));
                    }
                }

                var freeTables = $_data.find('.smartfreetable-preview');
                var hotTabDatas = [];
                var hotTabIds = [];
                var freeTabHtmls = [];
                for (var i = 0; i < freeTables.size(); i++) {
                    var freeTable = $(freeTables.get(i));
                    var id = freeTable.attr('id');

                    var obj = $_doc.data(id);
                    if(!obj){
                        ck.util.showMsg(2, '报表正在加载中，稍后保存');
                        return;
                    }
                    var hotDataObj = JSON.stringify(obj);
                    var hideTable = freeTable.find('.hideClass');
                    if (hideTable.length > 0) {
                        var freeTabHtml = hideTable[0].outerHTML;

                        hotTabIds.push(id);
                        hotTabDatas.push(encodeURIComponent(hotDataObj));
                        freeTabHtmls.push(encodeURIComponent(freeTabHtml.replace(/&nbsp;/g, '')));
                    }
                }

                $_data.find('.simplebox-content *').remove();
                $_data.find('span.excel_icon_export').remove();

                var pointIds = [];
                $_data.find('div.smartpoint').each(function(i, n) {
                    var id = $(n).data('tabid');
                    pointIds.push(id);
                })
                $.post('fastTemplate/saveReport', {
                    pageId: pageId,
                    data: encodeURIComponent($_data.html()),
                    ckHeight: height,
                    ckWidth: width,
                    hotTabIds: hotTabIds.join('@'),
                    hotTabDatas: hotTabDatas.join('@'),
                    freeTabHtmls: freeTabHtmls.join('@'),
                    pointIdStrs: pointIds.join(',')
                }).success(function() {
                    ck.util.showMsg(1, '报表保存成功');
                }).error(function() {
                    ck.util.showMsg(2, '报表保存失败');
                });
            }
        });
    }
});